/**
 * Created by ZY on 2018/01/06
 */
let _ = require('lodash'),
  util = require('../helper/util'),
  errCode = require('../../conf/errorCode'),
  logger = require('../helper/logger'),
  service = require('../services/postService');

var tagLogger = new logger.TagLogger('POST_CONTROLLER');

exports.addPost = function(req, res, next) {
  let {userId} = req.cookies;
  service.addPost(req.body, userId, util.getHandle(req, res, errCode.ADD_POST));
};

exports.addPostForUser = function(req, res, next) {
  let {userId} = req.cookies,
        {title,content} = req.body;
  service.addPostForUser(title,content, userId, util.getHandle(req, res, errCode.ADD_POST));
};

exports.updatePost = function(req, res, next) {
  let id = req.params.postId,
    {userId} = req.cookies;
  service.updatePost(
    id,
    req.body,
    userId,
    util.getHandle(req, res, errCode.ADD_POST),
  );
};

exports.deletePostByIds = function(req, res, next) {
  let ids = req.params.postId;
  service.deletePostByIds(
    ids,
    util.getHandle(req, res, errCode.DELETE_POST_BY_IDS),
  );
};

exports.getPostByPage = function(req, res, next) {
  let {page, size, channel = 0} = req.query;
  service.getPostByPage(
    channel,
    page,
    size,
    util.getHandle(req, res, errCode.GET_POST_BY_PAGE),
  );
};

exports.getPostById = function(req, res, next) {
  let {postId} = req.params;
  let {userId} = req.cookies;
  service.getPostById(
    postId,
    util.getHandle(req, res, errCode.GET_POST_BY_POST_ID),
  );
};

exports.getPostCategory = function(req, res, next) {
  service.getPostCategory(util.getHandle(req, res, errCode.GET_POST_CATEGORY));
};
